#include "main_window.hpp"


#include "about.hpp"
#include "palette_editor.hpp"
#include "stash.hpp"
#include "style.hpp"
#include "tile_editor.hpp"

#include <QApplication>
#include <QDir>
#include <QDockWidget>
#include <QGridLayout>
#include <QTabBar>
#include <QTabWidget>
#include <QWidget>


GesusMainWindow::
GesusMainWindow([[maybe_unused]] int     argc,
				[[maybe_unused]] char *  argv[])
	: gesusEditorTabs(this),
	  gesusMenuBar(this),
	  gesusTileEditor(this),
	  gesusTileStash(this),
	  gesusPaletteEditor(this)
{
	create_missing_dirs();
	
	setup_editor_tabs();
	
	setup_actions();
	
	setup_menus();
	
	setup_main_window();
}


void GesusMainWindow::
setup_main_window()
{
	gesusMenuBar.addMenu( &gesusFileMenu );
	gesusMenuBar.addMenu( &gesusViewMenu );

	gesusTileStashDock.setWidget(&gesusTileStash);
	
	gesusPaletteEditorDock.setWidget(&gesusPaletteEditor);

	addDockWidget( Qt::BottomDockWidgetArea, &gesusPaletteEditorDock, Qt::Horizontal );
	addDockWidget( Qt::BottomDockWidgetArea, &gesusTileStashDock,     Qt::Horizontal );
	
	setCentralWidget( &gesusEditorTabs );
	setMenuBar      ( &gesusMenuBar    );
}


void GesusMainWindow::
create_missing_dirs()
{
	QDir tileDir = gesusTileStash.get_tile_dir();

	tileDir.mkpath(TILE_STASH_DEFAULT_DIR);
}


void GesusMainWindow::
setup_editor_tabs()
{
	QTabBar * gesusTabBar;
	
	gesusEditorTabs.setMovable    ( true                   );
	gesusEditorTabs.setTabPosition( QTabWidget::North      );
	gesusEditorTabs.setTabShape   ( QTabWidget::Triangular );

	gesusTabBar = gesusEditorTabs.tabBar();
	gesusTabBar->setTabsClosable(true);
	
	// WIP: add icons
	gesusEditorTabs.addTab( &gesusPlaneEditor,  PLANE_EDITOR_TAB_NAME  );
	gesusEditorTabs.addTab( &gesusSpriteEditor, SPRITE_EDITOR_TAB_NAME );
	gesusEditorTabs.addTab( &gesusTileEditor,   TILE_EDITOR_TAB_NAME   );

	this->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::North);
}


void GesusMainWindow::
setup_menus()
{
	gesusFileMenu.addActions( {&actShowAbout,
							   &actExitApplication} );

	gesusViewMenu.addActions( {&actShowPaletteEditor,
							   &actShowTileStash} );
}


void GesusMainWindow::
setup_actions()
{
	actShowPaletteEditor.setCheckable(true);
	
	actShowTileStash.setCheckable(true);
	
	connect(&actShowPaletteEditor, &QAction::toggled, this,
		[=, this](bool checked) {
			this->slot_show_hide_dock(gesusPaletteEditorDock, checked);
		} );
	
	connect(&actShowTileStash, &QAction::toggled, this,
		[=, this](bool checked) {
			this->slot_show_hide_dock(gesusTileStashDock, checked);
		} );
	
	connect(&actExitApplication, &QAction::triggered, qApp,        &QApplication::quit);
	connect(&actShowAbout,       &QAction::triggered, &gesusAbout, &GesusAbout::show   );
	
	actExitApplication.setShortcut  ( kseqExitApplication   );
	actShowPaletteEditor.setShortcut( kseqShowPaletteEditor );
	actShowTileStash.setShortcut    ( kseqShowTileStash     );

	/*
	 *                ------------- Shortcuts -------------
	 */	
	connect(&scCloseTab, &QShortcut::activated, this,
		[this]() {
			int index = gesusEditorTabs.currentIndex();
			gesusEditorTabs.tabBar()->removeTab(index);
		} );
	
	connect(&scRightTab, &QShortcut::activated, this,
		[this]() {
			this->slot_select_adjacent_tab(this->scRightTab.key());
		} );

	connect(&scLeftTab, &QShortcut::activated, this,
		[this]() {
			this->slot_select_adjacent_tab(this->scLeftTab.key());
		} );
	
	connect(&scFileMenu, &QShortcut::activated, &gesusFileMenu,
		[this]() {
			this->gesusFileMenu.show();
		} );

	connect(&scFullscreen, &QShortcut::activated, this, &GesusMainWindow::slot_toggle_fullscreen);
}




